﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtorsPortal.Business_Logic_Layer;
using System.Data;

namespace RealtorsPortal
{
    public partial class Search : System.Web.UI.Page
    {
        string type = "";
        string title = "";
        string propertyType = "";
        string category = "";
        string city = "";
        string district = "";
        string areaFrom = "";
        string areaTo = "";
        string priceFrom = "";
        string priceTo = "";
        AdvertisementsDAL advd = new AdvertisementsDAL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["type"] != null)
            {
                type = Request.QueryString["type"].ToString();
                if (type == "basic")
                {
                    if (Request.QueryString["key"] != null)
                        title = Request.QueryString["key"].ToString();
                    DataTable dt = advd.SearchBasicAds(title);
                    lvwSearchAds.DataSource = dt;
                    lvwSearchAds.DataBind();
                    lblTotal.Text = dt.Rows.Count.ToString();
                }
                else
                {
                    string key = "";

                    if (Request.QueryString["propertyType"] != null)
                    {
                        propertyType = Request.QueryString["propertyType"].ToString();
                        key += " and a.type='" + propertyType + "'";
                    }
                    if (Request.QueryString["city"] != null)
                    {
                        city = Request.QueryString["city"].ToString();
                        key += " and c.CityId =" + city;
                    }
                    if (Request.QueryString["district"] != null)
                    {
                        district = Request.QueryString["district"].ToString();
                        key += " and a.DistrictId = " + district;
                    }
                    if (Request.QueryString["category"] != null)
                    {
                        category = Request.QueryString["category"].ToString();
                        key += " and a.CategoryId =" + category;
                    }
                    if (Request.QueryString["areafrom"] != null)
                    {
                        areaFrom = Request.QueryString["areafrom"].ToString();
                        key += " and a.LandArea >= " + areaFrom;
                    }
                    if (Request.QueryString["areato"] != null)
                    {
                        areaTo = Request.QueryString["areato"].ToString();
                        key += " and a.LandArea <= " + areaTo;
                    }
                    if (Request.QueryString["pricefrom"] != null)
                    {
                        priceFrom = Request.QueryString["pricefrom"].ToString();
                        key += " and a.Price >= "+ priceFrom;
                    }
                    if (Request.QueryString["priceto"] != null)
                    {
                        priceTo = Request.QueryString["priceto"].ToString();
                        key += " and a.Price <= "+ priceTo;
                    }
                    this.LoadAndBindData(key);
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        private void LoadAndBindData(string key)
        {
            DataTable dt = advd.SearchAdvancedAds(key);
            lblTotal.Text = dt.Rows.Count.ToString();
            lvwSearchAds.DataSource = dt;
            lvwSearchAds.DataBind();
        }
    }
}